package com.drop.action;

import com.drop.common.Util;
import com.drop.entity.Goods;
import com.drop.service.GoodsService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品表现层
 * Created by 陈东一 on 2017/9/23 9:48
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/goods")
@Scope("prototype")
public class GoodsAction extends ActionSupport{
	private Log log = LogFactory.getLog(this.getClass());
	@Autowired
	private GoodsService goodsService;

	/**
	 * 展示商品信息
	 * @return
	 * @throws Exception
	 */
	@Action(value = "introduction",results = {
			@Result(name="introduction",location = "/WEB-INF/home/introduction.jsp")
	})
	public String showGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String id = request.getParameter("itemid");
		Goods goods = goodsService.showGoods(Integer.parseInt(id));
		ActionContext.getContext().put("goods",goods);
		return "introduction";
	}

	/**
	 * 查找商品
	 * @return
	 */
	@Action(value = "search",results = {
			@Result(name="search",location = "/WEB-INF/home/search.jsp")})
	public String searchGoods(){
		HttpServletRequest request = ServletActionContext.getRequest();
		request=ServletActionContext.getRequest();
		String term = request.getParameter("term");
		List<Goods> list_goods = goodsService.searchGoodsByTerm(term);
		ActionContext.getContext().put("list_goods",list_goods);
		return "search";
	}


	/**
	 * 显示商品类别
	 * @return
	 * @throws Exception
	 */
	@Action(value = "type",results = {})
	public String showTypes() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		Util.returnJson(response, goodsService.showGoodsType());
		return null;
	}


	/**
	 * 显示商品列表
	 * @return
	 * @throws IOException
	 */
	@Action(value = "list",results = {})
	public String showAllGoods() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		Util.returnJson(response, goodsService.showAllGoods(1,page,rows));
		return null;
	}


	/**
	 * 添加商品
	 * @return
	 * @throws Exception
	 */
	@Action(value = "add",results = {})
	public String addGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String goodstype = request.getParameter("goodstype");
		String goodsName = request.getParameter("goodsName");
		String goodsContent = request.getParameter("goodsContent");
		String price = request.getParameter("price");
		String stock = request.getParameter("stock");
		String goodsImg = request.getParameter("goodsImg");
		Util.returnJson(response, goodsService.addGoods(goodstype,goodsName,goodsContent,price,stock,goodsImg,1));
		return null;
	}

	/**
	 * 修改商品信息
	 * @return
	 * @throws Exception
	 */
	@Action(value = "update",results = {})
	public String updateGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String goodstype = request.getParameter("goodstype");
		String goodsId = request.getParameter("goodsId");
		String goodsName = request.getParameter("goodsName");
		String goodsContent = request.getParameter("goodsContent");
		String price = request.getParameter("price");
		String stock = request.getParameter("stock");
		String goodsImg = request.getParameter("goodsImg");
		Util.returnJson(response, goodsService.updateGoods(goodsId,goodstype,goodsName,goodsContent,price,stock,goodsImg,1));
		return null;
	}

	/**
	 * 删除商品
	 * @return
	 * @throws Exception
	 */
	@Action(value = "delete",results = {})
	public String deleteGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String goodsId = request.getParameter("goodsId");
		Util.returnJson(response, goodsService.deleteGoods(goodsId));
		return null;
	}

}
